package com.sinodata.bsm.cicp.collector.windows.snmp;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;

import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.snmp.GetData;
import com.sinodata.bsm.cicp.util.snmp.MibDiskAndMemoEntry;
import com.sinodata.bsm.cicp.util.snmp.OMMappingInfo;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-8-27 下午5:27:25          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class MemoryCollector extends SnmpCollector {

    private static final Logger logger = Logger.getLogger(MemoryCollector.class);

    @Override
    public List<ResPropertyValue> collect(Snmp snmp, CommunityTarget target) throws CollectorException {

        /**
         * 内存使用率
         */
        int memoryUseRate = -1;

        /**
         *空闲内存大小，单位MB 
         */
        long freeMemorySize = -1;

        List<ResPropertyValue> resultList = new ArrayList<ResPropertyValue>();
        String[] props = this.getPropMeta();
        try {
            List<OMMappingInfo> memory = GetData.getAllTableData(snmp, MibDiskAndMemoEntry.class, target);
            if (memory.size() < 1) {
                throw new CollectorException(ip + " snmp没有获取到windows内存相关数据");
            }
            ResPropertyValue resPropertyValue = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[props.length];
            for (OMMappingInfo mapping : memory) {
                MibDiskAndMemoEntry memoryMapping = (MibDiskAndMemoEntry) mapping;
                if ("1.3.6.1.2.1.25.2.1.2".equals(memoryMapping.getHrStorageType())) {
                    memoryUseRate = memoryMapping.getHrStorageUsed() * 100 / memoryMapping.getHrStorageSize();
                    freeMemorySize = (100 - memoryUseRate) * memoryMapping.getHrStorageSize() / 100L * memoryMapping.getHrStorageAllocationUnits() / 1024L / 1024;
                }
            }
            if (memoryUseRate == -1 || freeMemorySize == -1) {
                throw new CollectorException(ip + " snmp 采集windows内存指标有其他oid，现在程序中使用的oid匹配不成功");
            }
            propValues[0] = ResPropertyValue.createPropValue(memoryUseRate + "", props[0]);
            propValues[1] = ResPropertyValue.createPropValue(freeMemorySize + "", props[1]);
            resPropertyValue.setValues(propValues);
            resultList.add(resPropertyValue);
        } catch (Exception e) {
            logger.error(ip + " windows内存指标采集失败", e);
            throw new CollectorException(e);
        } finally {
            if (snmp != null) {
                try {
                    snmp.close();
                } catch (Exception e2) {
                }
            }
        }
        return resultList;
    }

    @Override
    public String[] getPropMeta() {
        //内存使用率，空闲内存大小MB
        return new String[] { "$1", "$2" };
    }

    @Override
    public String[] getParamMeta() {
        return null;
    }

}
